<?php

session_start();

// include settings and functions
include "../conn/database.php";
include "functions.php";
include 'UUID.php';
// if the user submit the form
if(isset($_POST['submit'])) {
	// initialize errors values
	$errors=0;
	$errors_text = '';
	// cleanning the post data from hacking attempt
	$post_data = clean($_POST);
	$wid = $_SESSION['wid'];

	// empty post data verification
	if(empty($_POST['dd1']) || empty($_POST['mm1']) || empty($_POST['yyyy1'])
	|| empty($_POST['dd2']) || empty($_POST['mm2']) || empty($_POST['yyyy2'])
	|| empty($_POST['dd3']) || empty($_POST['mm3']) || empty($_POST['yyyy3']) ) {
		$errors_text .= "Des champs sont vides";
		$errors++;
	}
		
	// birthday verification (is valid date)
	if(!checkdate($_POST['mm1'], $_POST['dd1'], $_POST['yyyy1'])
	|| !checkdate($_POST['mm2'], $_POST['dd2'], $_POST['yyyy2'])
	|| !checkdate($_POST['mm3'], $_POST['dd3'], $_POST['yyyy3'])) {
		$errors_text .= "La date de naissance n'est pas valide";
		$errors++;
	}
	
	// if no errors we proceed
	if($errors == 0) {
		// data correction
		$Date1 = $post_data["yyyy1"].'-'.$post_data["mm1"].'-'.$post_data["dd1"];
		$Date2 = $post_data["yyyy2"].'-'.$post_data["mm2"].'-'.$post_data["dd2"];
		$Date3 = $post_data["yyyy3"].'-'.$post_data["mm3"].'-'.$post_data["dd3"];
		
		//insert in to t_weddings
		$insert_sql = "
			UPDATE `T_WEDDINGS` SET
			`WEDDING_DATE`='".$Date3."',`FIRST_MEETING`='".$Date1."',`PROPOSE_DATE`='".$Date2."' WHERE `WEDDING_ID`='".$wid."'"
		;

		if(!mysql_query($insert_sql)) {
			$errors_text .= mysql_error()."<br />";
			$errors++;
		}
	}
	if($errors == 0) {
		$arr['success'] = "1"; 
        $arr['msg'] = 'Vos informations ont bien été enregistrées.';
		$arr['url'] = 'histoire.php?step=1';
	} else {
		$arr['success'] = "0"; 
        $arr['msg'] = $errors_text;
	}
	echo json_encode($arr);
}
?>